package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbWorkFileBo;
import com.ruoyi.tb.lmt.domain.vo.TbWorkFileVo;
import com.ruoyi.tb.lmt.domain.TbWorkFile;
import com.ruoyi.tb.lmt.mapper.TbWorkFileMapper;
import com.ruoyi.tb.lmt.service.ITbWorkFileService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 大机履历资料Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-01
 */
@RequiredArgsConstructor
@Service
public class TbWorkFileServiceImpl implements ITbWorkFileService {

    private final TbWorkFileMapper baseMapper;

    /**
     * 查询大机履历资料
     */
    @Override
    public TbWorkFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询大机履历资料列表
     */
    @Override
    public TableDataInfo<TbWorkFileVo> queryPageList(TbWorkFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbWorkFile> lqw = buildQueryWrapper(bo);
        Page<TbWorkFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询大机履历资料列表
     */
    @Override
    public List<TbWorkFileVo> queryList(TbWorkFileBo bo) {
        LambdaQueryWrapper<TbWorkFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbWorkFile> buildQueryWrapper(TbWorkFileBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbWorkFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLmtRepairId()), TbWorkFile::getLmtRepairId, bo.getLmtRepairId());
        lqw.eq(bo.getOssId() != null, TbWorkFile::getOssId, bo.getOssId());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), TbWorkFile::getFileName, bo.getFileName());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), TbWorkFile::getUrl, bo.getUrl());
        lqw.eq(bo.getCreateTime() != null, TbWorkFile::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbWorkFile::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbWorkFile::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbWorkFile::getUpdateBy, bo.getUpdateBy());
        return lqw;
    }

    /**
     * 新增大机履历资料
     */
    @Override
    public Boolean insertByBo(TbWorkFileBo bo) {
        TbWorkFile add = BeanUtil.toBean(bo, TbWorkFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改大机履历资料
     */
    @Override
    public Boolean updateByBo(TbWorkFileBo bo) {
        TbWorkFile update = BeanUtil.toBean(bo, TbWorkFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbWorkFile entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除大机履历资料
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
